package front;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import prim.PRect;
import prim.Primitive;

public class SceneReader
{
    
    private static final String FILE_PATH = "scene/";
    private static final String FILE_TYPE = ".rtf";
    
    
    private List<String> lines;
    
    public SceneReader(String fileName)
    {
        this.lines = new ArrayList<>();
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(
                SceneReader.FILE_PATH + fileName + SceneReader.FILE_TYPE
            ));
            String in;
            while ((in = br.readLine()) != null)
            {
                in = in.trim();
                if (!in.isEmpty())
                {
                    this.lines.add(in);
                }
            }
            br.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
            System.exit(1);
        }
    }
    
    
    private static final int MODE_RECT = 0;
    
    private int index = 0;
    
    public Primitive getNextPrimitive()
    {
        int mode = -1;
        for (; index < this.lines.size(); index++)
        {
            String line = this.lines.get(index);
            
            if (line.contains("Rect"))
            {
                mode = SceneReader.MODE_RECT;
            }
            index++;
            
            
            if (mode == SceneReader.MODE_RECT)
            {
                PRect prim = new PRect();
                double ox = Double.valueOf(this.lines.get(index).split(" ")[1]);
                double oy = Double.valueOf(this.lines.get(index).split(" ")[2]);
                double oz = Double.valueOf(this.lines.get(index).split(" ")[3]);
                prim.setOrigin(new double[] { ox, oy, oz });
                index++;
                
                int axis = Integer.valueOf(this.lines.get(index).split(" ")[1]);
                ((PRect) prim).setNormalAxis(axis);
                index++;
                
                int dir = Integer.valueOf(this.lines.get(index).split(" ")[1]);
                ((PRect) prim).setNormalDirection(dir);
                index++;
                
                double dx = Double.valueOf(this.lines.get(index).split(" ")[1]);
                double dy = Double.valueOf(this.lines.get(index).split(" ")[2]);
                ((PRect) prim).setDimension(new double[] { dx, dy });
                index++;
                
                double cx = Double.valueOf(this.lines.get(index).split(" ")[1]);
                double cy = Double.valueOf(this.lines.get(index).split(" ")[2]);
                double cz = Double.valueOf(this.lines.get(index).split(" ")[3]);
                prim.setColor(new double[] { cx, cy, cz });
                index++;
                
                double refl = Double.valueOf(this.lines.get(index).split(" ")[1]);
                prim.setReflection(refl);
                index++;
                
                return prim;
            }
        }
        return null;
    }
    
}
